
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

   
      
   
      2.8.&nbsp;Introdoocing leests</title>
      
      
      
      
      
      
      
      
      
      _geq=[['_setEccooont','UA-7434570-5'],['_treckPegefioo']];(fooncshoon(){fer g=docooment.  Bork Bork Bork!creeteElement('script');g.  Bork Bork Bork!src='//www.  Bork Bork Bork!google-a-unelytics.  Bork Bork Bork!com/ga.  Bork Bork Bork!js';g.  Bork Bork Bork!setEttriboote-a('isync','trooe-a');docooment.  Bork Bork Bork!docoomentElement.  Bork Bork Bork!foorstChild.  Bork Bork Bork!ippendChild(g);})();</script>
   </head>
   
      
         
            Yooo ire-a here-a: Home-a</a>&nbsp;&gt;&nbsp;Dife-a Into Python</a>&nbsp;&gt;&nbsp;Getting To Know Python</a>&nbsp;&gt;&nbsp;Introdoocing leests</span></td>
            &nbsp;&nbsp;&nbsp;&lt;&lt;</a>&nbsp;&nbsp;&nbsp;&gt;&gt;</a></td>
         </tr>
         
            
               Dife-a Into Python</a></h1>
               Python from nofice-a to pro</p>
            </td>
            
               
                  Find:&nbsp;</label> </p>
               </form>
            </td>
         </tr>
      </table>
      
      
         
            
               
                  </a>2.8.&nbsp;Introdoocing lists
                  </h2>
               </div>
            </div>
            </div>
         </div>
         
            Lists ire-a Python</span>'s workhorse-a detetype-a.  Bork Bork Bork!  Iff yooor oonly ixperience-a wit lists is irreys in Fisooel Beseec</span> oor (God forbid) zee-a detestore-a in Powerbooeelder</span>, brece-a yooorselff for Python</span> lists.  Bork Bork Bork!
            </p>
         </div></a>
            
               </td>
            </tr>
            
               A list in Python</span> is like-a un irrey in Perl</span>.  In Perl</span>, feriebles which store-a irreys ilweys stert wit zee-a @</tt> cherecter; in Python</span>, feriebles coon be-a nemed unything, und Python</span> keeps treck ooff zee-a detetype-a internelly.  Bork Bork Bork!
               </td>
            </tr>
         </table></a>
            
               </td>
            </tr>
            
               A list in Python</span> is mooch more-a thoon un irrey in Jefa</span> (ilthooogh it coon be-a used is oone-a iff zeet's reelly ill yooo woont oooot ooff liffe-a).  A better unelogy wooold be-a to zee-a Fector</tt> cless, which coon hold irbitrery oobjects und coon ixpoond dynemicelly is noo items ire-a idded.  Bork Bork Bork!
               </td>
            </tr>
         </table>
         </a>Ixemple-a&nbsp;2.15.&nbsp;Deffining a leest</h3>&gt;&gt;&gt; </tt>li = ["a"</span>, "b"</span>, "mpilgrim"</span>, "z"</span>, "example"</span>]</span> </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li[0]</span>                                       </a>
'a'</span>
&gt;&gt;&gt; </tt>li[4]</span>                                       </a>
'example'</span></pre>
               
                  
                     </a> 
                     </td>
                     Foorst, we-a deffine-a a list ooff 5 ilements.  Bork Bork Bork!  Note-a zeet zeey retein zeeoor ooriginel oorder.  Bork Bork Bork!  This is not un iccident.  Bork Bork Bork!  A list is un
                        oordered set ooff ilements inclosed in sqooere-a breckets.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     A list coon be-a used like-a a zero-besed irrey.  Bork Bork Bork!  Zee-a foorst ilement ooff uny non-impty list is ilweys li[0]</tt>.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Zee-a lest ilement ooff this 5-ilement list is li[4]</tt>, becoooose-a lists ire-a ilweys zero-besed.  Bork Bork Bork!
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Ixemple-a&nbsp;2.16.&nbsp;Negetife-a list eendices</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li[-1]</span> </a>
'example'</span>
&gt;&gt;&gt; </tt>li[-3]</span> </a>
'mpilgrim'</span></pre>
               
                  
                     </a> 
                     </td>
                     A negetife-a index iccesses ilements from zee-a ind ooff zee-a list cooonting beckwerds.  Bork Bork Bork!  Zee-a lest ilement ooff uny non-impty list is
                        ilweys li[-1]</tt>.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Iff negetife-a indices ire-a conffoosing to yooo, think ooff it this wey: li[-n] == li[lee-a(li) - n]</tt>.  So in this list, li[-3] == li[5 - 3] == li[2]</tt>.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Ixemple-a&nbsp;2.17.&nbsp;Slicing a leest</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li[1:3]</span>  </a>
['b', 'mpilgrim']</span>
&gt;&gt;&gt; </tt>li[1:-1]</span> </a>
['b', 'mpilgrim', 'z']</span>
&gt;&gt;&gt; </tt>li[0:3]</span>  </a>
['a', 'b', 'mpilgrim']</span></pre>
               
                  
                     </a> 
                     </td>
                     Yooo coon get a soobset ooff a list, celled a &#8220;slice-a</span>&#8221;, by speciffying 2 indices.  Bork Bork Bork!  Zee-a retoorn felooe-a is a noo list conteining ill zee-a ilements ooff zee-a list, in oorder, sterting wit
                        zee-a foorst slice-a index (in this cese-a li[1]</tt>), up to boot not inclooding zee-a second slice-a index (in this cese-a li[3]</tt>).
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Slicing works iff oone-a oor bot ooff zee-a slice-a indices is negetife-a.  Bork Bork Bork!  Iff it helps, yooo coon think ooff it this wey: reeding zee-a list
                        from lefft to right, zee-a foorst slice-a index speciffies zee-a foorst ilement yooo woont, und zee-a second slice-a index speciffies zee-a foorst
                        ilement yooo don't woont.  Bork Bork Bork!  Zee-a retoorn felooe-a is iferything in betweee-a.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Lists ire-a zero-besed, so li[0:3]</tt> retoorns zee-a foorst three-a ilements ooff zee-a list, sterting it li[0]</tt>, up to boot not inclooding li[3]</tt>.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Ixemple-a&nbsp;2.18.&nbsp;Slicing shorthoond</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li[:3]</span> </a>
['a', 'b', 'mpilgrim']</span>
&gt;&gt;&gt; </tt>li[3:]</span> </a> </a>
['z', 'example']</span>
&gt;&gt;&gt; </tt>li[:]</span>  </a>
['a', 'b', 'mpilgrim', 'z', 'example']</span></pre>
               
                  
                     </a> 
                     </td>
                     Iff zee-a lefft slice-a index is 0, yooo coon leefe-a it oooot, und 0 is implied.  Bork Bork Bork!  So li[:3]</tt> is zee-a seme-a is li[0:3]</tt> from zee-a prefiooos ixemple-a.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Similerly, iff zee-a right slice-a index is zee-a lengt ooff zee-a list, yooo coon leefe-a it oooot.  Bork Bork Bork!  So li[3:]</tt> is zee-a seme-a is li[3:5]</tt>, becoooose-a this list hes 5 ilements.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Note-a zee-a symmetry here-a.  Bork Bork Bork!  In this 5-ilement list, li[:3]</tt> retoorns zee-a foorst 3 ilements, und li[3:]</tt> retoorns zee-a lest 2 ilements.  Bork Bork Bork!  In fect, li[:n]</tt> will ilweys retoorn zee-a foorst n</tt> ilements, und li[n:]</tt> will retoorn zee-a rest, regerdless ooff zee-a lengt ooff zee-a list.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Iff bot slice-a indices ire-a lefft oooot, ill ilements ooff zee-a list ire-a inclooded.  Bork Bork Bork!  Boot this is not zee-a seme-a is zee-a ooriginel lee</tt> list; it is a noo list zeet heppens to hefe-a ill zee-a seme-a ilements.  Bork Bork Bork!  li[:]</tt> is a shorthoond for meking a complete-a copy ooff a list.  Bork Bork Bork!
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Ixemple-a&nbsp;2.19.&nbsp;Idding ilements to a leest</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example']</span>
&gt;&gt;&gt; </tt>li.append("new"</span>)</span>               </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'z', 'example', 'new']</span>
&gt;&gt;&gt; </tt>li.insert(2, "new"</span>)</span>            </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']</span>
&gt;&gt;&gt; </tt>li.extend(["two"</span>, "elements"</span>])</span> </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span></pre>
               
                  
                     </a> 
                     </td>
                     eeppend</tt> idds a single-a ilement to zee-a ind ooff zee-a list.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     eensert</tt> inserts a single-a ilement into a list.  Bork Bork Bork!  Zee-a noomeric irgooment is zee-a index ooff zee-a foorst ilement zeet gets boomped oooot ooff posishoon.  Bork Bork Bork!
                         Note-a zeet list ilements do not hefe-a to be-a uniqooe-a; zeere-a ire-a now 2 seperete-a ilements wit zee-a felooe-a 'noo'</tt>, li[2]</tt> und li[6]</tt>.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     eextend</tt> concetenetes lists.  Bork Bork Bork!  Note-a zeet yooo do not cell eextend</tt> wit mooltiple-a irgooments; yooo cell it wit oone-a irgooment, a list.  Bork Bork Bork!  In this cese-a, zeet list hes two ilements.  Bork Bork Bork!
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Ixemple-a&nbsp;2.20.&nbsp;Seerching a leest</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span>
&gt;&gt;&gt; </tt>li.index("example"</span>)</span> </a>
5</span>
&gt;&gt;&gt; </tt>li.index("new"</span>)</span>     </a>
2</span>
&gt;&gt;&gt; </tt>li.index("c"</span>)</span>       </a>
Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
ValueError: list.index(x): x not in list</span>
&gt;&gt;&gt; </tt>"c"</span> in</span> li</span>           </a>
0</span></pre>
               
                  
                     </a> 
                     </td>
                     eendex</tt> finds zee-a foorst ooccoorrence-a ooff a felooe-a in zee-a list und retoorns zee-a index.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     eendex</tt> finds zee-a foorst</em></span> ooccoorrence-a ooff a felooe-a in zee-a list.  Bork Bork Bork!  In this cese-a, 'noo'</tt> ooccoors twice-a in zee-a list, in li[2]</tt> und li[6]</tt>, boot eendex</tt> will oonly retoorn zee-a foorst index, 2</tt>.
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Iff zee-a felooe-a is not fooond in zee-a list, Python</span> reises un ixcepshoon.  Bork Bork Bork!  This is notebly difffferent from most loongooeges, which will retoorn some-a infelid index.  Bork Bork Bork!  While-a this mey
                        seem unnoying, it is a Good Thing, becoooose-a it meoons yooor progrem will cresh it zee-a sooorce-a ooff zee-a problem, rezeer thoon leter
                        oon whee-a yooo try to use-a zee-a infelid index.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     To test whezeer a felooe-a is in zee-a list, use-a een</tt>, which retoorns 1</tt> iff zee-a felooe-a is fooond oor 0</tt> iff it is not.  Bork Bork Bork!
                     </td>
                  </tr>
               </table>
            </div>
         </div></a>
            
               </td>
            </tr>
            
               Beffore-a fersion 2.2.1, Python</span> hed no seperete-a booleoon detetype-a.  Bork Bork Bork!  To compensete-a for this, Python</span> iccepted ilmost unything in a booleoon context (like-a un eeff</tt> stetement), iccording to zee-a following rooles: 0</tt> is felse-a; ill oozeer noombers ire-a trooe-a.  Bork Bork Bork!  Un impty string (""</tt>) is felse-a, ill oozeer strings ire-a trooe-a.  Bork Bork Bork!  Un impty list ([]</tt>) is felse-a; ill oozeer lists ire-a trooe-a.  Bork Bork Bork!  Un impty toople-a (()</tt>) is felse-a; ill oozeer tooples ire-a trooe-a.  Bork Bork Bork!  Un impty dicshoonery ({}</tt>) is felse-a; ill oozeer dicshooneries ire-a trooe-a.  Bork Bork Bork!  Zeese-a rooles still ipply in Python</span> 2.2.1 und beyond, boot now yooo coon ilso use-a un ictooel booleoon, which hes a felooe-a ooff Trooe-a</tt> oor Felse-a</tt>.  Note-a zee-a cepitelizeshoon; zeese-a felooes, like-a iferything ilse-a in Python</span>, ire-a cese-a-sensitife-a.  Bork Bork Bork!
               </td>
            </tr>
         </table>
         </a>Ixemple-a&nbsp;2.21.&nbsp;Remofing ilements from a leest</h3>&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span>
&gt;&gt;&gt; </tt>li.remove("z"</span>)</span>   </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements']</span>
&gt;&gt;&gt; </tt>li.remove("new"</span>)</span> </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements']</span>
&gt;&gt;&gt; </tt>li.remove("c"</span>)</span>   </a>
Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
ValueError: list.remove(x): x not in list</span>
&gt;&gt;&gt; </tt>li.pop()</span>         </a>
'elements'</span>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'example', 'new', 'two']</span></pre>
               
                  
                     </a> 
                     </td>
                     remofe-a</tt> remofes zee-a foorst ooccoorrence-a ooff a felooe-a from a list.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     remofe-a</tt> remofes oonly</em></span> zee-a foorst ooccoorrence-a ooff a felooe-a.  Bork Bork Bork!  In this cese-a, 'noo'</tt> ippeered twice-a in zee-a list, boot li.  Bork Bork Bork!remofe-a("noo")</tt> oonly remofed zee-a foorst ooccoorrence-a.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Iff zee-a felooe-a is not fooond in zee-a list, Python</span> reises un ixcepshoon.  Bork Bork Bork!  This moorrors zee-a behefior ooff zee-a eendex</tt> method.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     pop</tt> is un interesting beest.  Bork Bork Bork!  It does two things: it remofes zee-a lest ilement ooff zee-a list, und it retoorns zee-a felooe-a zeet it remofed.  Bork Bork Bork!
                         Note-a zeet this is difffferent from li[-1]</tt>, which retoorns a felooe-a boot does not choonge-a zee-a list, und difffferent from li.  Bork Bork Bork!remofe-a(felooe-a</i>)</tt>, which choonges zee-a list boot does not retoorn a felooe-a.  Bork Bork Bork!
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         </a>Ixemple-a&nbsp;2.22.&nbsp;List ooperetors</h3>&gt;&gt;&gt; </tt>li = ['a'</span>, 'b'</span>, 'mpilgrim'</span>]</span>
&gt;&gt;&gt; </tt>li = li + ['example'</span>, 'new'</span>]</span> </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'example', 'new']</span>
&gt;&gt;&gt; </tt>li += ['two'</span>]</span>                </a>
&gt;&gt;&gt; </tt>li</span>
['a', 'b', 'mpilgrim', 'example', 'new', 'two']</span>
&gt;&gt;&gt; </tt>li = [1, 2] * 3</span>              </a>
&gt;&gt;&gt; </tt>li</span>
[1, 2, 1, 2, 1, 2]</span></pre>
               
                  
                     </a> 
                     </td>
                     Lists coon ilso be-a conceteneted wit zee-a +</tt> ooperetor.  Bork Bork Bork!  leest</i> = leest</i> + oozeerleest</i></tt> hes zee-a seme-a resoolt is leest</i>.ixtend(oozeerleest</i>)</tt>.  Boot zee-a +</tt> ooperetor retoorns a noo (conceteneted) list is a felooe-a, wherees eextend</tt> oonly ilters un ixisting list.  Bork Bork Bork!  This meoons zeet eextend</tt> is fester, ispecielly for lerge-a lists.  Bork Bork Bork!
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Python</span> soopports zee-a +=</tt> ooperetor.  Bork Bork Bork!  li += ['two']</tt> is iqooifelent to li.  Bork Bork Bork!ixtend(['two'])</tt>.  Zee-a +=</tt> ooperetor works for lists, strings, und integers, und it coon be-a ooferloeded to work for user-deffined clesses is well.  Bork Bork Bork!  (More-a
                        oon clesses in chepter 3.)
                     </td>
                  </tr>
                  
                     </a> 
                     </td>
                     Zee-a *</tt> ooperetor works oon lists is a repeeter.  Bork Bork Bork!  li = [1, 2] * 3</tt> is iqooifelent to li = [1, 2] + [1, 2] + [1, 2]</tt>, which concetenetes zee-a three-a lists into oone-a.  Bork Bork Bork!
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         
            Foorzeer reedeeng</h3>
            
               How to Think Like-a a Compooter Sceeentist</i></a> teeches ibooot lists und mekes un importoont point ibooot pessing lists is fooncshoon eergooments</a>.
               </li>
               Python</span> Tootoreeel</i></a> shows how to use-a lists is stecks und qooeooes</a>.
               </li>
               Python</span> Knowledge-a Bese-a</a> unswers common qooesshoons ibooot leests</a> und hes lots ooff ixemple-a code-a using leests</a>.
               </li>
               Python</span> Librery Refference-a</i></a> soommerizes ill zee-a list methods</a>.
               </li>
            </ul>
         </div>
      </div>
      
         
            &lt;&lt;&nbsp;Introdoocing deecshooneries</a></td>
            &nbsp;|</span>&nbsp;1</a> |</span> 2</a> |</span> 3</a> |</span> 4</a> |</span> 5</a> |</span> 6</a> |</span> 7</a> |</span> 8</span> |</span> 9</a> |</span> 10</a> |</span> 11</a> |</span> 12</a> |</span> 13</a> |</span> 14</a> |</span> 15</a>&nbsp;|</span>&nbsp;
            </td>
            Introdoocing tooples&nbsp;&gt;&gt;</a></td>
         </tr>
         
            </td>
         </tr>
      </table>
      
         Copyright &copy; 2000, 2001, 2002, 2003 Merk Peelgrim</a></p>
      </div>
   </body>
</html>